package WaitNotify;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WaitAndSleep {
    //final不可变 ，锁住同一个锁对象，避免引用被修改
    static Object obj = new Object();
    public static void main(String[] args) {

        new Thread(new Runnable() {
            public void run() {
                synchronized (obj) {
                    log.debug("t1线程获得锁");
                    try {
                        //sleep不会释放锁，wait会释放锁，通过比较主线程获得锁的时间
                        //sleep是Thread方法，wait是Object方法
                        //sleep不需要强制配合synchronized方法使用，wait需要
                        //调用后线程会进入TIMED_WAITING状态
                        Thread.sleep(20000);

                        //obj.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        },"t1").start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        synchronized (obj) {
            log.debug("主线程已经获取锁");
        }

    }
}
